<template>
  <div v-loading="loading">
    <div>
      <el-row>
        <el-col :span="20">
          <el-form v-model="search" :inline="true">
            <el-form-item>
              <el-input placeholder="输入搜索关键字" v-model="search.name"
                        clearable/>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="load">查询</el-button>
              <el-button type="info" @click="reset">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="4" style="text-align: right">
          <el-button size="mini" type="success" @click="addHandle" v-if="hasAuth('sys:user:save')">新增</el-button>
        </el-col>
      </el-row>
    </div>

    <el-table
        ref="userTable"
        :data="tableData"
        style="width: 100%"
        border>
      <el-table-column
          label="头像"
          align="center"
          width="50">
        <template slot-scope="scope">
          <el-avatar size="small" :src="scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>

      <el-table-column
          prop="username"
          label="用户名"
          align="center"
          width="120">
      </el-table-column>
      <el-table-column
          prop="code"
          align="center"
          label="角色名称">
        <template slot-scope="scope">
          <el-tag size="small" type="info" v-for="item in scope.row.sysRoles">{{ item.name }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column
          prop="email"
          align="center"
          label="邮箱">
      </el-table-column>

      <el-table-column
          prop="city"
          align="center"
          label="所在城市">
      </el-table-column>

      <el-table-column
          prop="statu"
          align="center"
          label="状态">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.statu === 1" type="success">正常</el-tag>
          <el-tag size="small" v-else-if="scope.row.statu === 0" type="danger">禁用</el-tag>
        </template>
      </el-table-column>

      <el-table-column
          prop="created"
          width="200"
          align="center"
          :formatter="dateFormat"
          label="创建时间">
      </el-table-column>

      <el-table-column
          prop="icon"
          width="260px"
          align="center"
          label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="roleHandle(scope.row.id)">分配角色</el-button>
          <el-divider direction="vertical"></el-divider>

          <el-button type="text" @click="repassHandle(scope.row.id, scope.row.username)">重置密码</el-button>
          <el-divider direction="vertical"></el-divider>

          <el-button type="text" @click="editHandle(scope.row.id)">编辑</el-button>
          <el-divider direction="vertical"></el-divider>

          <template>
            <el-popconfirm title="确定删除吗？" @confirm="delHandle(scope.row.id)">
              <el-button type="text" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </template>
      </el-table-column>

    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[5,10,20]"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="total">
    </el-pagination>

    <add-user ref="addUser" @handle="handleChild"></add-user>
    <assign-role ref="assignRole" @handle="handleChild"></assign-role>
  </div>
</template>

<script>
import addUser from "./AddUser";
import assignRole from "./AssignRole";
import {getUserList, delUserById, resetPasswd} from "@/api/sys/user";

export default {
  name: "User",
  components: {addUser, assignRole},
  data() {
    return {
      loading: false,
      search: {},
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
    }
  },
  methods: {
    load() {
      const params = {
        name: this.search.name,
        pageSize: this.pageSize,
        currentPage: this.currentPage
      }
      getUserList(params).then(res => {
        this.tableData = res.data.records
        this.tableData.forEach(data => {
          if (data.avatar !== null) {
            data.avatar = "/api" + data.avatar
          }
        })
        this.pageSize = res.data.size
        this.currentPage = res.data.current
        this.total = res.data.total
      })
    },
    reset() {
      this.search = {}
      this.pageSize = 5
      this.currentPage = 1
      this.load()
    },
    //
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage
      this.load()
    },
    //分配角色
    roleHandle(id) {
      this.$refs.assignRole.openDialog(id)
    },
    delHandle(id) {
      delUserById(id).then(res => {
        if (res.code === 200) {
          this.load()
        }
      })
    },
    //重置密码
    repassHandle(id, username) {
      this.$confirm('将重置用户【' + username + '】的密码, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //重置密码
        resetPasswd(id).then(res => {
          this.$message({
            type: "success",
            message: "重置密码成功"
          })
          this.load()
        })
      })
    },
    //编辑
    editHandle(id) {
      this.$refs.addUser.setEditForm(id)
      this.$refs.addUser.openDialog()
    },
    //新增
    addHandle() {
      this.$refs.addUser.openDialog()
    },
    handleChild(res) {
      if (res.code === 200) {
        this.load()
      }
    },

    /*日期处理*/
    dateFormat(row, column) {
      let date = row[column.property];
      if (date === undefined) {
        return ''
      }
      return this.$moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
  },
  created() {
    this.load()
  }
}
</script>

<style scoped>

</style>
